﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events;
using myapp.Instances;
using myapp.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;

namespace myapp.ViewModels
{
    [Export(typeof(IShell))]
    public class ShellViewModel : Screen, IShell
    {
        #region  Attribute
        /// <summary>
        /// Window Manager
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// Event Aggregator
        /// </summary>
        private readonly IEventAggregator _eventAggregator;

        /// <summary>
        /// List person
        /// </summary>
        private IObservableCollection<Person> _lstPersion;

        #endregion

        #region Properties

        public IObservableCollection<Person> ListPerSon
        {
            get
            {
                if (!_lstPersion.Equals(null))
                {
                    return _lstPersion;
                }
                else
                {
                    _lstPersion = new BindableCollection<Person>();
                    return _lstPersion;
                }
            }
            set
            {
                if (!value.Equals(null))
                {
                    _lstPersion = value;
                    NotifyOfPropertyChange(() => ListPerSon);
                }
            }
        }

        #endregion

        #region Contructor
        /// <summary>
        /// Contructor
        /// </summary>
        /// <param name="windowManager"></param>
        /// <param name="eventAggregator"></param>
        [ImportingConstructor]
        public ShellViewModel(IWindowManager windowManager, IEventAggregator eventAggregator)
        {
            DisplayName = "Person Manager";
            this._windowManager = windowManager;
            this._eventAggregator = eventAggregator;
            this._eventAggregator.Subscribe(this);

            //Set MainInstance
            MainInstance.Instance.WindowManager = windowManager;
            MainInstance.Instance.EventAggregator = eventAggregator;

            
            OnInit();
        }
        #endregion

        #region Method

        /// <summary>
        /// Init ViewModel
        /// </summary>
        private void OnInit()
        {
            _lstPersion = new BindableCollection<Person>();
            _lstPersion.Add(new Person() { UserName = "vuhln", PassWord = "abc" });
            _lstPersion.Add(new Person() { UserName = "fool", PassWord = "abc" });
        }



        /// <summary>
        /// Add person
        /// </summary>
        public void AddPerson()
        {
            //_windowManager.ShowWindow(new DialogViewModel(this._eventAggregator));           
            //_windowManager.ShowWindow(new PossitonManagerViewModel());
            List<PossitionGroupModel> lstGroupModel = PossitionDAO.ListPossitionGroup();
            _windowManager.ShowWindow(new PossitonManagerViewModel());
        }



        #endregion

    }
}
